package com.deep.coupon.service.impl;

import com.deep.common.model.dto.MemberPriceVO;
import com.deep.common.model.dto.SkuFullReductionTO;
import com.deep.coupon.model.entity.MemberPriceEntity;
import com.deep.coupon.model.entity.SkuLadderEntity;
import com.deep.coupon.service.MemberPriceService;
import com.deep.coupon.service.SkuLadderService;
import org.apache.shiro.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deep.common.utils.PageUtils;
import com.deep.common.utils.Query;

import com.deep.coupon.dao.SkuFullReductionDao;
import com.deep.coupon.model.entity.SkuFullReductionEntity;
import com.deep.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSkuReductionTO(SkuFullReductionTO skuFullReductionTO) {
        Assert.notNull(skuFullReductionTO, "skuFullReductionTO 不能为空!");
        // 保存sku的优惠、满减、会员价格等信息

        // 1、保存商品优惠信息
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuFullReductionTO.getSkuId());
        skuLadderEntity.setFullCount(skuFullReductionTO.getFullCount());
        skuLadderEntity.setDiscount(skuFullReductionTO.getDiscount());
        // 是否参加其他优惠
        skuLadderEntity.setAddOther(skuFullReductionTO.getCountStatus());
        // 有的满减条件才保存
        if (skuFullReductionTO.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
        skuLadderService.save(skuLadderEntity);

        // 2、保存商品满减信息
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuFullReductionTO, skuFullReductionEntity);
        if ((skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) > 0)) {
            this.save(skuFullReductionEntity);
        }

        // 3、保存价格等属性
        List<MemberPriceVO> memberPrice = skuFullReductionTO.getMemberPrice();
        if(memberPrice!=null&&memberPrice.size()>0){

            List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(m -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuFullReductionTO.getSkuId());
                memberPriceEntity.setMemberLevelId(m.getId());
                memberPriceEntity.setMemberLevelName(m.getName());
                memberPriceEntity.setMemberPrice(m.getPrice());
                memberPriceEntity.setAddOther(1);

                return memberPriceEntity;
            }).filter(item ->
                    // 输入的商品价格必须要大于0才保存
                    (item.getMemberPrice().compareTo(new BigDecimal("0")) > 0)
            ).collect(Collectors.toList());

            memberPriceService.saveBatch(memberPriceEntities);
        }

    }

}